package com.example.tests;

import static com.example.tests.GroupDataGenerator.loadGroupsFromCsvFile;
import static com.example.tests.GroupDataGenerator.loadGroupsFromXmlFile;
import static org.junit.Assert.assertThat;
import static org.hamcrest.Matchers.*;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import com.example.utils.SortedListOf;

public class GroupCreationTests extends TestBase {
	
	@DataProvider
  public Iterator<Object[]> groupsFromFile() throws IOException{
  	return wrapGroupDataForDataProvider(loadGroupsFromXmlFile(new File("groups.xml"))).iterator();
  }

	@Test(dataProvider = "groupsFromFile")
  public void testGroupCreationWithValidData(GroupData group) throws Exception {
    
    //save old state
//    SortedListOf<GroupData> oldList = new SortedListOf<GroupData>(app.getHibernateHelper().listGroups());
		SortedListOf<GroupData> oldList = app.getModelGroups().getGroups();
    
    // actions
    app.getGroupHelper().createGroup(group);
    
    // save new state
//    SortedListOf<GroupData> newList = app.getGroupHelper().getGroups();
    SortedListOf<GroupData> newList = app.getModelGroups().getGroups();
    
    // compare state states before and after
    assertThat(newList, equalTo(oldList));
    
    // compare model to implementation
    if (wantToCheck()) {
      if ("yes".equals(app.getProperty("check.db"))) {
        assertThat(app.getModelGroups().getGroups(), equalTo(app.getHibernateHelper().listGroups()));
        System.out.println("DB check done. On group: " + group.getGrName());
      }
      
      if ("yes".equals(app.getProperty("check.ui"))) {
        assertThat(app.getModelGroups().getGroups(), equalTo(app.getGroupHelper().getUiGroups()));
        System.out.println("UI check done. On group: " + group.getGrName());
      }
    }
  }

}
